/**
 * created by zhouli on 2018/12/20
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import {permissionTypeList,} from 'containers/permission-setting/permission-setting.model';
export default {
  //获取权限类别
  getPermissionCategory(){
    return new Promise((resolve) => {
      resolve(permissionTypeList)
    })
  },
  //获取职能（角色）列表
  getRoleList() {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/security/permission/resource/tenant')
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //新增职能
  createRole(role) {
    // let role = {
    //   "tenantId":985801821654523905,
    //   "permissionName":"集团管理员",
    //   "permissionCode":"tenant_admin",
    //   "permissionDesc":"集团的管理员",
    //   "permissionType":"ROLE_TENANT_ADMIN",    //-------与 ROLE_TENANT_ADMIN，ROLE_COMPANY_FINANCE_ADMIN 对应 , 先写死，之后提供值列表
    //   "permissionCategory":"财务",
    //   "enabled":true
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/security/permission/resource', role)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //修改职能
  updateRole(role) {
    // let role = {
    //   "id": "1077167530856054786",
    //   "tenantId": "985801821654523905",
    //   "permissionName": "公司管理员",
    //   "permissionCode": "company_admin1",
    //   "permissionDesc": "公司的管理员",
    //   "permissionType": "ROLE_COMPNAY_ADMIN",
    //   "permissionCategory":"管理员",
    //   "enabled": true,
    //   "deleted": false
    // }
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/security/permission/resource', role)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //获取默认的激活的key
  //可以指定激活的菜单 v_item
  /*old-Version*/
  getSelectedKeys(list, v_item){
    let selectedKeys = [];
    let openKeys = [];
    let dataActive = null;
    if (!v_item && list.length && list.length > 0) {
      let s1 = [];
      let s2 = [];
      let s3 = [];
      //todo
      //角色列表分类目前前端写死的，分为3类
      //如果后期类别有增多，前端可以根据后端返回的类别列表，进行分类，
      list.map(item => {
        if (item.permissionCategory === 'ADMIN') {
          s1.push(item)
        } else if (item.permissionCategory === 'FINANCE') {
          s2.push(item)
        } else {
          s3.push(item)
        }
      });
      if (s1.length > 0) {
        openKeys = ['ADMIN'];
        selectedKeys = [s1[0].id + ""];
        dataActive = s1[0];
      }
      if (s1.length === 0 && s2.length > 0) {
        openKeys = ['FINANCE'];
        selectedKeys = [s2[0].id + ""];
        dataActive = s2[0];
      }
      if (s1.length === 0 && s2.length === 0) {
        openKeys = ['OTHER'];
        selectedKeys = [s3[0].id + ""];
        dataActive = s3[0];
      }
    }
    //如果有指定的角色
    if (v_item) {
      openKeys = [v_item.permissionCategory];
      selectedKeys = [v_item.id + ""];
      dataActive = v_item;
    }
    return [selectedKeys, openKeys, dataActive]
  },

  //获取默认的激活的key
  //可以指定激活的菜单 v_item
  /*new-Version-wangruonan*/
  getSelectedKeysAbandon(list, v_item){
    let roleArr = ["ADMIN", "SUP_ADMIN", "SUB_ADMIN", "FINANCE", "OTHER"];
    let selectedKeys = [], openKeys = [], keysArr = [], dataActive = null;
    if (!v_item && list.length && list.length > 0) {
      roleArr.map(role =>{
        let arr = list.filter(item => item.permissionCategory === role);
        if(arr && arr.length){
          keysArr.push(arr[0]);
        }
      });
      openKeys = [keysArr[0].permissionCategory];
      selectedKeys = [keysArr[0].id + ""];
      dataActive = keysArr[0];
      }
    if (v_item) {
      openKeys = [v_item.permissionCategory];
      selectedKeys = [v_item.id + ""];
      dataActive = v_item;
    }
    return [selectedKeys, openKeys, dataActive];
  }
}
